/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rentcalculator.model;

import java.util.Iterator;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

/**
 *
 * @author K.E.Y
 */

@Entity
public class Charge {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private long id;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="bill_id")
    private Bill bill;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="person_id")
    private Person person;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="algorithm_id")
    private Algorithm algorithm;
    
    private double fee;

    @Column(nullable = true)
    private String comment;

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
    
    public double getFee() {
        return fee;
    }

    public void setFee(double fee) {
        this.fee = fee;
    }
    
    @OneToMany(fetch = FetchType.EAGER)
    private List<Discount> offsets;

    public Charge() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Bill getBill() {
        return bill;
    }

    public void setBill(Bill bill) {
        this.bill = bill;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Algorithm getAlgorithm() {
        return algorithm;
    }

    public void setAlgorithm(Algorithm algorithm) {
        this.algorithm = algorithm;
    }

    public List<Discount> getOffsets() {
        return offsets;
    }

    public void setOffsets(List<Discount> offsets) {
        this.offsets = offsets;
    }
    
    public double getActualFee(){
        List<Discount> discounts = this.getPerson().getBillOffsets();
        double actualFee = this.getFee();
        if(discounts == null ) return actualFee;
        Iterator<Discount> iter = discounts.iterator();
        while(iter.hasNext()){
            Discount tmp = iter.next();
            actualFee = tmp.discount(actualFee);
            
            
            
        }
        
        return actualFee;
        
    }
}
